#!/bin/bash
#
# Copyright 2015-2017 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# === Standalone Source Helper ===
# source <(curl -Ls https://github.com/AdrianDC/android_development_shell_tools/raw/master/android_kernel_builders.rc)
# source <(curl -Ls https://github.com/AdrianDC/android_development_shell_tools/raw/master/extensions/sony_msm8996/android_kernel_builders.rc)

# === Sony Copyleft Kernel Builder ===
function makekernelcopyleft()
{
  # Out path handling
  if [ ! -d ./.build ]; then
    mkdir ./.build;
  fi;

  # Mode: make clean / make mrproper
  if [ "${1}" = 'clean' ] || [ "${1}" = 'mrproper' ]; then

    # Launch make mode
    make "${1}" O=./.build;
    return;
  fi;

  # Environment
  if [ -z "${KBUILD_DIFFCONFIG}" ] || [ ! -z "${1}" ]; then

    # Usage
    if [ -z "${1}" ] || ! ls 'arch/arm'*"/configs/diffconfig/${1}_diffconfig" 1> /dev/null 2>&1; then
      echo '';
      echo ' Usage: makekernelcopyleft [platform_device_to_init / clean / mrproper] [make_parameters] (Kernel inline compiler for Sony Copyleft)';
      echo '   Details: The kernel needs to be under an Android build tree to select toolchains'
      echo '';
      return;
    fi;

    # Kernel defconfig
    export KBUILD_DIFFCONFIG=${1}_diffconfig;
    kerneldefconfig 'msm-perf';

    # Kernel toolchains selection
    kerneltoolchains;

    # Kernel configuration
    make "${DEFCONFIG_NAME}" O=./.build;
  fi;

  # Variables
  local time_diff;
  local make_parameters=${*:2};

  # Kernel build
  time_diff=$(date +%s);
  make_parameters="makes ${make_parameters}";
  ${make_parameters} O=./.build;

  # Show compilation time
  time_diff=$(($(date +%s) - time_diff));
  echo '';
  echo -e " \e[1;37m[ Done compiling in ${time_diff} secs ]\e[0m";
  echo '';
}

# === Sony Copyleft Kernel Merger ===
function kernelcopyleftmerger()
{
  # Usage: kernelcopyleftmerger (Sony Copyleft kernel archive merge helper)

  # Stash all lost .gitignore files
  echo '';
  echo ' kernelcopyleftmerger: Stashing all lost .gitignore files';
  git --no-pager diff --name-only | grep -ai .gitignore | while IFS='' read -r line; do
    git checkout HEAD "${line}";
  done;

  # List all real file differences
  echo '';
  echo ' kernelcopyleftmerger: Listing all relevant changes';
  git --no-pager -c core.fileMode=false diff --name-only;

  # Add all new changes
  echo '';
  echo ' kernelcopyleftmerger: Adding all relevant changes';
  git -c core.fileMode=false add -Av;
  echo '';

  # Commit the merge
  echo '';
  echo ' kernelcopyleftmerger: Commit the changes';
  git commit -s;

  # Clean unmerged files
  echo '';
  echo ' kernelcopyleftmerger: Cleaning unmerged files';
  git reset HEAD --hard;
  git stash -u;
}
